/* -*- C++ -*- */

/****************************************************************************
 ** Copyright (c) quickfixengine.org  All rights reserved.
 **
 ** This file is part of the QuickFIX FIX Engine
 **
 ** This file may be distributed under the terms of the quickfixengine.org
 ** license as defined by quickfixengine.org and appearing in the file
 ** LICENSE included in the packaging of this file.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ** See http://www.quickfixengine.org/LICENSE for licensing information.
 **
 ** Contact ask@quickfixengine.org if any conditions of this licensing are
 ** not clear to you.
 **
 ****************************************************************************/

#ifndef FAST_PARSER_H
#define FAST_PARSER_H

#ifdef _MSC_VER
#pragma warning( disable : 4503 4355 4786 4290 )
#endif

#include "Exceptions.h"
#include <iostream>
#include <sstream>
#include <string>

#include "Parser.h"
#include "common.h"
#include "FASTApi.h"
#include "deque"

//Group = Len (u32) + PMap + TemplateID (u32)

namespace FIX {
/// Parses %FAST messages off an input stream.
class FASTParser: public Parser {
public:
	FASTParser() :
		Parser() {
	}

	~FASTParser() {
	}

	void reset(void);

	void resetTemplate(int tid);

	bool extractLength(int& length, std::string::size_type& pos,
			const std::string& buffer) throw (MessageParseError);

	bool readMessage(std::string& str) throw (MessageParseError);

	virtual void readFromStream(int m_socket) throw (SocketRecvFailed);

	virtual void writeMessage(const std::string& str) throw (MessageParseError);

	virtual bool writeToStream(int m_socket) throw (SocketRecvFailed);

	virtual void readFromStream( int  m_socket,  sockaddr_in &addr) throw ( SocketRecvFailed );
	virtual bool writeToStream( int  m_socket, const sockaddr_in &addr) throw ( SocketRecvFailed );

private:

	void decodeTags(int tid, std::stringstream & ss, std::string msg_type = "");
	void encodeTags(int tid, std::map<int, std::string> tagvalues);
	void encodeTags(int tid, const FIX::Message &msg, int groupCount = -1);

	Queue iq;
	static Mutex iq_mutex;

	FASTApi iFast, oFast;
};
}
#endif //FAST_PARSER_H
